package com.tomasio.projects.welcome.integration
{
	
	import com.tomasio.projects.welcome.events.CameraServiceEvent;
	import com.tomasio.projects.welcome.model.CameraIP;
	import com.tomasio.projects.welcome.model.Chave;
	
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.rpc.AsyncToken;
	import mx.rpc.Responder;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.mxml.RemoteObject;
	
	[Event(name="cameraLoadedEvent", type="com.tomasio.projects.welcome.events.CameraServiceEvent")]
	[Event(name="camerasLoadedEvent", type="com.tomasio.projects.welcome.events.CameraServiceEvent")]
	[Event(name="cameraRemovedEvent", type="com.tomasio.projects.welcome.events.CameraServiceEvent")]
	[Event(name="cameraCreatedEvent", type="com.tomasio.projects.welcome.events.CameraServiceEvent")]
	[Event(name="cameraUpdatedEvent", type="com.tomasio.projects.welcome.events.CameraServiceEvent")]
	
	
	[Bindable]
	public class CameraService extends RemoteObject
	{
		public function CameraService()
		{
			super("camerasServiceDestination");
		}
	
		public function call(Method:AsyncToken, NewResult:Function):void{  
			var L_Async:AsyncToken;  
			L_Async = Method;  
			L_Async.addResponder(new Responder(NewResult, DefaultFaultHandler));  
		}  
		public function get getConexao():RemoteObject{  
			return this;  
		}  

		public function findAll():void{
			this.call(this.getConexao.findAllCameras(), findAllResultHandler);
		}
		public function create(camera:CameraIP):void{
			this.call(this.getConexao.createCamera(camera), createResultHandler);	
			
		}
		public function update(camera:CameraIP):void{
			this.call(this.getConexao.updateCamera(camera), updateResultHandler);	
			
		}

		public function find(id:Number):void{
			this.call(this.getConexao.findCamera(id), findResultHandler);
		}
		public function remove(id:Number):void{
			this.call(this.getConexao.removeCamera(id), removeResultHandler);
		}
				
		
		
		/*
		**
		** Handlers
		**
		*/
		
		private function findAllResultHandler(event:ResultEvent):void {
			var cameras:ArrayCollection = event.result as ArrayCollection;
			this.dispatchEvent(
				new CameraServiceEvent(CameraServiceEvent.CAMERAS_LOADED_EVENT, null, cameras));
		}
		private function DefaultFaultHandler(Result:FaultEvent):void{     
			Alert.show(Result.fault.faultString,"Error");  
		} 
		private function createResultHandler(event:ResultEvent):void {
			this.dispatchEvent(
				new CameraServiceEvent(CameraServiceEvent.CAMERA_CREATED_EVENT, null, null));
			
		}
		private function findResultHandler(event:ResultEvent):void {
			var camera:CameraIP = event.result as CameraIP;
			this.dispatchEvent(
				new CameraServiceEvent(CameraServiceEvent.CAMERA_LOADED_EVENT, camera, null));
			
		}
		private function removeResultHandler(event:ResultEvent):void{
			this.dispatchEvent(
				new CameraServiceEvent(CameraServiceEvent.CAMERA_REMOVED_EVENT, null, null));

		}
		private function updateResultHandler(event:ResultEvent):void{
			this.dispatchEvent(
				new CameraServiceEvent(CameraServiceEvent.CAMERA_UPDATED_EVENT, null, null));

		}

		
	}	
}